<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3D 立体轮播-原理剖析</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .slider {
            width: 600px;
            height: 300px;
            margin: 150px auto;
            position: relative;
        }
        .slider-outer{
            /*-webkit-transform-style: preserve-3d;*/
            /*-moz-transform-style: preserve-3d;*/
            /*-ms-transform-style: preserve-3d;*/
            /*transform-style: preserve-3d;*/
            /*transform: rotateX(-30deg) rotateY(10deg);*/
        }
        .slider-outer, .slider-item, .slider-item .img {
            width: 100%;
            height: 100%;
            position: absolute;
        }
        .slider-item{
            width: 100px;
            /*border: 1px solid red;*/
            -webkit-transform-style: preserve-3d;
            -moz-transform-style: preserve-3d;
            -ms-transform-style: preserve-3d;
            transform-style: preserve-3d;
            transition: 2s;
        }
        .img:nth-child(1){
            background-image: url("images/1-1.jpg");
            transform:  rotateY(0deg) translateZ(50px);
        }
        .img:nth-child(2){
            background-image: url("images/1-2.jpg");
            transform:  rotateY(90deg) translateZ(50px);
        }
        .img:nth-child(3){
            background-image: url("images/1-3.jpg");
            transform:  rotateY(180deg) translateZ(50px);
        }
        .img:nth-child(4){
            background-image: url("images/1-4.jpg");
            transform:  rotateY(270deg) translateZ(50px);
        }

        .btns {
            width: 100%;
            height: 50px;
            position: absolute;
            top: 50%;
            margin-top: -25px;
            font-size: 30px;
            color: #ffffff;
            text-align: center;
            line-height: 50px;
        }
        .prev, .next {
            position: absolute;
            background-color: rgba(0, 0, 0, .3);
            width: 50px;
            height: 100%;

        }
        .prev {
            left: 0;
        }
        .next {
            right: 0;
        }
    </style>
</head>
<body>
<div class="slider">
    <div class="slider-outer">
        <div class="slider-item">
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
        </div>
        <div class="slider-item">
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
        </div>
        <div class="slider-item">
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
        </div>
        <div class="slider-item">
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
        </div>
        <div class="slider-item">
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
        </div>
        <div class="slider-item">
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
            <div class="img"></div>
        </div>
    </div>
    <div class="btns">
        <div class="prev" id="prev">&lt;</div>
        <div class="next" id="next">&gt;</div>
    </div>
</div>
<script>
    var items= document.getElementsByClassName("slider-item"),num=0;
    for(var i=0;i<items.length;i++){
        var item = items[i];
        var w=item.offsetWidth;
        item.style["left"]=i*w+"px";
        item.style["transitionDelay"]=i*.2+"s";
        var child=item.children;
        for(var j=0;j<child.length;j++){
            var img=child[j];
            img.style['backgroundPosition']=-i*w+"px";
        }
    }
    document.getElementById("prev").addEventListener('click', function () {
        var rotate=--num*90;
        for(var i=0;i<items.length;i++){
             items[i].style["transform"]="rotateY("+rotate+"deg)"
        }
    });

    document.getElementById("next").addEventListener('click', function () {
        var rotate=++num*90;
        for(var i=0;i<items.length;i++){
            items[i].style["transform"]="rotateY("+rotate+"deg)"
        }
    });


</script>
</body>
</html>